package alipay.openplatform.edu.campus.quartz.api.controller;

import alipay.openplatform.edu.campus.jiuye.service.iservice.message.IMessageService;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.io.Serializable;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by wangdan on 16/9/24.
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class SmsTimer extends QuartzJobBean implements Serializable {


    @Autowired
    private IMessageService messageService;

    private static Logger LOG = LoggerFactory.getLogger(SmsTimer.class);


    static int TIME = 1;

    private final Lock lock = new ReentrantLock();// 锁

    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        while (TIME == 1) {
            lock.lock();
            try {
                messageService.sendMessageToUser();
                LOG.info("定时任务发送短信|SmsTimer|executeInternal|params:No");
            } catch (Exception e) {
                LOG.error("定时任务发送短信|SmsTimer|executeInternal|params:error={}", e);
            } finally {
                TIME++;
                lock.unlock();
            }
        }
    }


}
